package com.cloud.job.report;

import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.cloud.api.domain.algorithm.VehiclePoint;
import com.cloud.api.domain.report.*;
import com.cloud.api.utils.ExcelUtils;
import com.cloud.job.service.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * @author 杨斌
 * 2025-02-2025-02-12
 */
@Service
@Slf4j
public class ExportService {

    @Resource
    private IVehicleMessageDailyService vehicleMessageDailyService;

    @Resource
    private IVehicleAlarmService vehicleAlarmService;

    @Resource
    private IArealDistributionService arealDistributionService;

    @Resource
    private IEvStatisticsService evStatisticsService;

    @Resource
    private EmailService emailService;

    @Resource
    private MessageSource messageSource;
    @JobExecutor(name = "export_vehicle_report")
    public void exportAllToSingleExcel() {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "vehicle_report";
        fileName = messageSource.getMessage(fileName, null, fileName, locale);

        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            // 创建一个包含多个Sheet的Workbook
            Workbook workbook = new SXSSFWorkbook();

            // 获取离线车辆数据并写入第一个Sheet
            List<VehicleOffline> offlineData = vehicleMessageDailyService.getVehicleOfflineData();
            ExcelUtils.addExportXSSFWorkbook(workbook, "离线车辆", offlineData, VehicleOffline.class);

            // 获取报警报告数据并写入第二个Sheet

            List<VehicleAlarmReport> alarmReportData = vehicleAlarmService.getAlarmReportData();
            ExcelUtils.addExportXSSFWorkbook(workbook, "报警数据", alarmReportData, VehicleAlarmReport.class);

            // 获取区域分布数据并写入第三个Sheet
            List<ArealDistributionExcel> arealDistributionData = arealDistributionService.getArealDistributionData();
            ExcelUtils.addExportXSSFWorkbook(workbook, "区域分布（省份）", arealDistributionData, ArealDistributionExcel.class);

            // 省市车辆运行情况
            List<ProvinceDistribution> provinceDistributions = evStatisticsService.getProvinceDistributionList();
            ExcelUtils.addExportXSSFWorkbook(workbook, "省市运行情况", provinceDistributions, ProvinceDistribution.class);
            // 车辆上线情况
            List<VehiclesOnLine> vehiclesOnLines = evStatisticsService.getVehiclesOnLineList();
            ExcelUtils.addExportXSSFWorkbook(workbook, "车辆上线情况", vehiclesOnLines, VehiclesOnLine.class);

            // 车型运行情况
            List<VehicleModelCondition> vehiclesModelList = evStatisticsService.getMailVehiclesModelList();
            ExcelUtils.addExportXSSFWorkbook(workbook, "车型运行情况", vehiclesModelList, VehicleModelCondition.class);

            //车辆能耗数据
            List<VehiclesEnergy> vehiclesEnergies = evStatisticsService.getVehiclesEnergyList();
            ExcelUtils.addExportXSSFWorkbook(workbook, "能耗数据分析（月）", vehiclesEnergies, VehiclesEnergy.class);

            List<VehiclePoint> pointList = vehicleMessageDailyService.getAggVehicleMessage();
            ExcelUtils.addExportXSSFWorkbook(workbook, "集中停放", pointList, VehiclePoint.class);

            // 将Workbook写入输出流
            workbook.write(outputStream);
            byte[] fileBytes = outputStream.toByteArray();

            // 异步发送邮件
            emailService.sendEmailAsync("车辆统计分析报表", "请查看附件中的车辆统计分析报表。", fileName + ".xlsx", fileBytes);

        } catch (IOException e) {
            log.error("导出Excel文件失败", e);
        }
    }
}
